package cn.edu.sdjzu.xg.bysj.service;
//201902104061徐中堂
import cn.edu.sdjzu.xg.bysj.dao.*;
import cn.edu.sdjzu.xg.bysj.domain.Student;
import cn.edu.sdjzu.xg.bysj.domain.Teacher;
import cn.edu.sdjzu.xg.bysj.domain.User;
import exception.BysjException;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

public class StudentService {
    //获取studentDao实例
    private StudentDao studentDao = StudentDao.getInstance();
    //新建StudentService对象
    private static StudentService studentService = new StudentService();
    //获取StudentService对象
    public static StudentService getInstance(){
        return studentService;
    }

    /**
     * 根据id查找
     * @param id
     * @return
     * @throws SQLException
     */
    public Student find(int id) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        Student student = null;
        //关闭自动提交，事务开始
        connection.setAutoCommit(false);
        try {
            student = this.studentDao.find(connection,id);
            //提交事务
            connection.commit();
        }
        catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        }
        finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return student;
    }

    /**
     * 条件分页查询
     * @param conditions
     * @param pagination
     * @return
     * @throws SQLException
     */
    public Collection<Student> findAll(Collection<Condition> conditions , Pagination pagination) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        Collection<Student> students = null;
        //关闭自动提交，事务开始
        connection.setAutoCommit(false);
        try {
            students = this.studentDao.findAll(connection,conditions,pagination);
            //提交事务
            connection.commit();
        }
        catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        }
        finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return students;
    }

    /**
     * 查询老师下属学生 分页
     * @param teacher
     * @param pagination
     * @return
     * @throws SQLException
     */
    public Collection<Student> findAll(Teacher teacher,Pagination pagination) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        Collection<Student> students = null;
        //关闭自动提交，事务开始
        connection.setAutoCommit(false);
        try {
            students = this.studentDao.findAll(connection,teacher,pagination);
            //提交事务
            connection.commit();
        }
        catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        }
        finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return students;
    }

    /**
     * 添加
     * @param student
     * @return
     * @throws SQLException
     */
    public boolean add(Student student) throws SQLException ,BysjException{
        //获得进行事务的链接，添加学生和用户都是用本连接对象
        Connection connection = JdbcHelper.getConn();
        //关闭自动提交，开始事务
        connection.setAutoCommit(false);
        boolean added = false;
        try {
            int actor_id = ActorDao.getInstance().add(connection);
            student.setId(actor_id);
            //添加学生对象
            StudentDao.getInstance().add(connection,student);
            //创建用户对象，用户名与密码取自student的字段值no
            User user = new User(student.getNo(),student.getNo(),null,student);
            //增加用户
            added = UserDao.getInstance().add(connection,user);
            ActorDao.getInstance().update(connection,student);
            ActorAssRoleDao.getInstance().add(connection,actor_id,student.getRoles());
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚所有的操作
            connection.rollback();
            throw new BysjException("新添加的学生的编号已经存在" + e.getMessage());
        }finally {
            //开启自动提交，结束事务
            connection.setAutoCommit(true);
            //关闭数据库连接
            JdbcHelper.close(null,connection);
        }
        return added;
    }

    /**
     * 更新
     * @param student
     * @return
     * @throws SQLException
     */
    public boolean update(Student student) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        boolean updated = false;
        //关闭自动提交，事务开始
        connection.setAutoCommit(false);
        try {
            updated = studentDao.update(connection,student);
            //提交事务
            connection.commit();
        }
        catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        }
        finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return updated;
    }

    /**
     * 删除
     * @param id
     * @return
     * @throws SQLException
     */
    public boolean delete(int id) throws SQLException{
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        boolean deleted = false;
        //关闭自动提交，事务开始
        connection.setAutoCommit(false);
        try {
            deleted = studentDao.deleted(connection,id);
            //提交事务
            connection.commit();
        }
        catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        }
        finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return deleted;
    }
}
